﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using Ventas.Api.Models;
using Ventas.Data.Entities;
using Ventas.Data.Managers;

namespace Ventas.Api.Controllers
{
    public class ProductController : ApiController
    {
        //
        public IEnumerable<ProductModel> Get()
        {
            return ProductManager.GetList("").Select(t => new ProductModel
            {
                Id = t.Id,
                Codigo = t.Codigo,
                Description = t.Description,
                Stock = t.Stock,
                Price = t.Price,
                FechaVenci = t.FechaVenci,

                CategoryId = t.CategoryId,
                CategoryModel = new CategoryModel
                {
                    Id = t.Category.Id,
                    Name = t.Category.Name,
                    Description = t.Category.Description,
                },
            });
        }
        //
        public string Get(int id)
        {
            return "value";
        }
        //
        public ProductModel Post([FromBody]ProductModel m)
        {
            Product t = ProductManager.Create(new Product
            {
                Codigo = m.Codigo,
                Description = m.Description,
                Stock = m.Stock,
                Price = m.Price,
                FechaVenci = m.FechaVenci,
                CategoryId = m.CategoryId,
            });
            return new ProductModel
            {
                Id = t.Id,
                Description = t.Description,
                Stock = t.Stock,
                Price = t.Price,
                FechaVenci = t.FechaVenci,

                CategoryId = t.CategoryId,
                CategoryModel = new CategoryModel
                {
                    Id = t.Category.Id,
                    Name = t.Category.Name,
                    Description = t.Category.Description,
                },
            };
        }
        //
        public ProductModel Put(Guid id, [FromBody]ProductModel m)
        {
            Product t = ProductManager.Update(new Product
            {
                Id = id,
                Codigo = m.Codigo,
                Description = m.Description,
                Stock = m.Stock,
                Price = m.Price,
                FechaVenci = m.FechaVenci,

                CategoryId = m.CategoryId,
            });
            return new ProductModel
            {
                Id = t.Id,
                Codigo = t.Codigo,
                Description = t.Description,
                Stock = t.Stock,
                Price = t.Price,
                FechaVenci = t.FechaVenci,

                CategoryId = t.CategoryId,
                CategoryModel = new CategoryModel
                {
                    Id = t.Category.Id,
                    Name = t.Category.Name,
                    Description = t.Category.Description,
                },
            };
        }
        //
        public void Delete(Guid id)
        {
            ProductManager.Delete(id);
        }
    }
}